package cn.piesat.service.impl;

import cn.piesat.dao.PiesatExterloginConfigDAO;
import cn.piesat.dto.PiesatExterloginConfigDTO;
import cn.piesat.service.intf.PiesatExterloginConfigService;
import cn.piesat.util.JedisUtils;
import cn.piesat.util.PageUtil;
import cn.piesat.util.config.FieldStaticConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 	第三方登录配置信息  服务实现
 * @author 奚好雷
 *
 */
@Service
public class PiesatExterloginConfigServiceImpl implements PiesatExterloginConfigService {
	
	@Autowired
	private PiesatExterloginConfigDAO dao;

	@Override
	public PiesatExterloginConfigDTO add(PiesatExterloginConfigDTO dto) {
		PiesatExterloginConfigDTO reDTO = dao.save(dto);
		return reDTO;
	}

	@Override
	public List<PiesatExterloginConfigDTO> findByPlatFormAndEnableState(String platForm, Integer enableState) {
		List<PiesatExterloginConfigDTO> list = dao.findByPlatFormAndEnableState(platForm, enableState);
		return list;
	}

	@Override
	public PiesatExterloginConfigDTO update(PiesatExterloginConfigDTO dto) {
		Optional<PiesatExterloginConfigDTO> optional = dao.findById(dto.getId());
		if(optional.isPresent()) {
			return null;
		}
		PiesatExterloginConfigDTO reDTO = dao.save(dto);
		Jedis jedis = JedisUtils.getJedis();
		Map<String, String> map = new HashMap<>();
		try {
			if (reDTO.getEnableState() == 0) {
				switch (reDTO.getPlatForm()) {
				case "wechat":
					map.put("appId", reDTO.getAppId());
					map.put("secret", reDTO.getSecret());
					jedis.hmset(FieldStaticConfig.WECHAT_REDIS_KEY, map);
					break;
				case "qq":
					map.put("appId", reDTO.getAppId());
					map.put("secret", reDTO.getSecret());
					jedis.hmset(FieldStaticConfig.QQ_REDIS_KEY, map);
					break;
				case "alipay":
					map.put("appId", reDTO.getAppId());
					map.put("secret", reDTO.getSecret());
					map.put("privateSecret", reDTO.getPrivateSecret());
					jedis.hmset(FieldStaticConfig.ALIPAY_REDIS_KEY, map);
					break;
				default:
					break;
				}
			} else if (reDTO.getEnableState() == 1) {
				List<PiesatExterloginConfigDTO> list = dao.findByPlatFormAndEnableState(reDTO.getPlatForm(), 0);
				if(list.isEmpty()) {
					switch (reDTO.getPlatForm()) {
					case "wechat":
						jedis.del(FieldStaticConfig.WECHAT_REDIS_KEY);
						break;
					case "qq":
						jedis.del(FieldStaticConfig.QQ_REDIS_KEY);
						break;
					case "alipay":
						jedis.del(FieldStaticConfig.ALIPAY_REDIS_KEY);
						break;
					default:
						break;
					}
				}
			}
		} finally {
			JedisUtils.close(jedis);
		}
		return reDTO;
	}

	@Override
	public Map<String,Object> findAll(Integer pageNum,Integer pageSize) {
		Sort sort = Sort.by(Sort.Direction.ASC,"platForm");
		Pageable pageable = PageRequest.of(pageNum,pageSize,sort);
		Page<PiesatExterloginConfigDTO> page = dao.findAll(pageable);
		return PageUtil.getPage(page);

	}

	@Override
	public Map<String, Object> findAllPage(Integer page,Integer size) {
		Pageable pageable = PageRequest.of(page, size);
		Page<PiesatExterloginConfigDTO> pageObject = dao.findAll(pageable);
		return PageUtil.getPage(pageObject);
	}

	@Override
	public void delById(String id) {
		PiesatExterloginConfigDTO dto = dao.findById(id).get();
		dao.deleteById(id);
		if(dto.getEnableState() == 0) {
			Jedis jedis = JedisUtils.getJedis();
			try {
				switch (dto.getPlatForm()) {
				case "wechat":
					jedis.del(FieldStaticConfig.WECHAT_REDIS_KEY);
					break;
				case "qq":
					jedis.del(FieldStaticConfig.QQ_REDIS_KEY);
					break;
				case "alipay":
					jedis.del(FieldStaticConfig.ALIPAY_REDIS_KEY);
					break;
				default:
					break;
				}
			} finally {
				JedisUtils.close(jedis);
			}
		}
	}

	@Override
	public List<PiesatExterloginConfigDTO> findByEnableState(Integer enableState) {
		return dao.findByEnableState(enableState);
	}

	@Override
	public List<PiesatExterloginConfigDTO> findByPlatForm(String platForm) {
		return dao.findByPlatForm(platForm);
	}
	
	
}
